<?php
include "./include/om_config.inc";

$smarty = new SmartyWWW();
$expiryDate = "";
$filePath = "bhavcopies";
$fileName = "om.txt";
$handle = '';
$generalQuery = "SELECT * FROM general";
$generalResult = mysql_query($generalQuery);
if($generalRow = mysql_fetch_array($generalResult))
{
  $fileName = $generalRow['fileName'];
  $year = substr($generalRow['fileName'],6,4);
  $month = substr($generalRow['fileName'],10,2);
  $date = substr($generalRow['fileName'],12,2);
  $sDate = date("Y-m-d",mktime(0,0,0,$month,$date,$year));
  
  //Check that data already in bhavcopy table :Start
  $storedbhavQuery = "SELECT * FROM storedbhav
                      WHERE storDate = '".$sDate."'";
  $storedbhavResult = mysql_query($storedbhavQuery);
	if($storedbhavRow = mysql_fetch_array($storedbhavResult))
	{
	  if($storedbhavRow['status'] == "Ok")
      $varStoreBhav = false;
    else
    {
      $bhavcopyDelete = "DELETE FROM bhavcopy WHERE bhavcopyDate = '".$sDate."'";
      $bhavcopyDeleteResult = mysql_query($bhavcopyDelete); 
      $varStoreBhav = true;
    }
	}
	else
	{
    $insertStor = "INSERT INTO storedbhav (storDate,status)
    							                 VALUES ('".$sDate."','started')";
    $insertStorResult = mysql_query($insertStor);	
	  $varStoreBhav = true;
	}
  //Check that data already in bhavcopy table :End
  
  if($varStoreBhav)
  {
    $row = 0;
    $scLineCount = 1;
    $FOLineCount = 1;
    $NSELineCount = 1;
    $handle = fopen($filePath."/".$fileName, "r");
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
    {
    	$num = count($data);
    	$exchange					=	'MCX';
    	$bhavcopyDate 		= $data[0];
    	$contractCode 		= $data[5];
    	$expiryDateBc 		= $data[6];
    	$openPrice 				= $data[10];
    	$openPrice  	= $data[11];
    	$highPrice  	= $data[12];
    	$lowPrice  		= $data[13];
    	$closePrice = $data[14];
    	$row++;
    	
   		$monthStr = substr($data[0],2,3);
    	switch ($monthStr)
    	{
    	  case "JAN":
    	    $monthInt = '01';
    	    break;
    	  case "FEB":
    	    $monthInt = '02';
    	    break;
    	  case "MAR":
    	    $monthInt = '03';
    	    break;
    	  case "APR":
    	    $monthInt = '04';
    	    break;
    	  case "MAY":
    	    $monthInt = '05';
    	    break;
    	  case "JUN":
    	    $monthInt = '06';
    	    break;
    	  case "JUL":
    	    $monthInt = '07';
    	    break;
    	  case "AUG":
    	    $monthInt = '08';
    	    break;
    	  case "SEP":
    	    $monthInt = '09';
    	    break;
    	  case "OCT":
    	    $monthInt = '10';
    	    break;
    	  case "NOV":
    	    $monthInt = '11';
    	    break;
    	  case "DEC":
    	    $monthInt = '12';
    	    break;
    	}
    	
   		$bhavcopyDate = substr($data[0],5,4)."-".$monthInt."-".substr($data[0],0,2);
////	////////For Expiry:Start
   		if(strlen(trim($data[6])) > 0)
   		{
	  	 	$monthStrExp = substr($data[6],2,3);
	  	  switch ($monthStrExp)
	  	  {
	  	    case "JAN":
	  	      $monthIntExp = '01';
	  	      break;
	  	    case "FEB":
	  	      $monthIntExp = '02';
	  	      break;
	  	    case "MAR":
	  	      $monthIntExp = '03';
	  	      break;
	  	    case "APR":
	  	      $monthIntExp = '04';
	  	      break;
	  	    case "MAY":
	  	      $monthIntExp = '05';
	  	      break;
	  	    case "JUN":
	  	      $monthIntExp = '06';
	  	      break;
	  	    case "JUL":
	  	      $monthIntExp = '07';
	  	      break;
	  	    case "AUG":
	  	      $monthIntExp = '08';
	  	      break;
	  	    case "SEP":
	  	      $monthIntExp = '09';
	  	      break;
	  	    case "OCT":
	  	      $monthIntExp = '10';
	  	      break;
	  	    case "NOV":
	  	      $monthIntExp = '11';
	  	      break;
	  	    case "DEC":
	  	      $monthIntExp = '12';
	  	      break;
	  	  }
	  	  $data[6];
	  	  $expiryDate = substr($data[6],5,4)."-".$monthIntExp."-".substr($data[6],0,2);
    	}
    	$atp = $data[23];
 			$insertDenied = 0;
 			$noDuplication ="SELECT contractCode FROM bhavcopy WHERE bhavcopyDate='".$bhavcopyDate."' 
 			                             AND contractCode='".$contractCode."' AND 
 			                             expiryDate='".$expiryDate."'";
			$resultNoDuplicate = mysql_query($noDuplication); 
			if($resultNoDuplicate && mysql_num_rows($resultNoDuplicate)!=0)
			{
				$insertDenied = 1;
			}
			if($highPrice == 0)
			{
			  $insertDenied = 1;
			}
			if($insertDenied == 0)
			{  
			  $insertData = "INSERT INTO bhavcopy (exchange,bhavcopyDate,contractCode,
				 																			expiryDate,expiryDateBc,openPrice,highPrice,lowPrice,closePrice,atp)
				               VALUES ('".$exchange."','".$bhavcopyDate."','".$contractCode."',
				                       '".$expiryDate."','".$expiryDateBc."',
				 											 ".$openPrice.",".$highPrice.",".$lowPrice.",".$closePrice.",".$atp.")";
			  $resultData = mysql_query($insertData);					
			}
    }
    $updateStorK = "UPDATE storedbhav SET status = 'Ok' 
                     WHERE storDate = '".$sDate."'";
    $updateStorResultK = mysql_query($updateStorK);	
    fclose($handle);
  }
  header("Location: ./txtFile.php");
}
?>